<?php
class masterbank_model extends CI_Model{
	private $kodebank;
    private $bankname;
    private $address;
    private $notlp;
  	private $norekening;
    private $createdby;
    private $createdon;
    private $lastupdateby;
  	private $lastupdateon;
  	private $active;
	
	function setKodeBank($kbank){
		$this->kodebank = $kbank;
	}

	function getKodeBank(){
		return $this->kodebank;
	}
	function setBankName($bname){
		$this->bankname = $bname;
	}

	function getBankName(){
		return $this->bankname;
	}
	
	function setAddress($addr){
		$this->address = $addr;
	}

	function getAddress(){
		return $this->address;
	}
	
	function setNoTelp($notelp){
		$this->notlp = $notelp;
	}

	function getNoTelp(){
		return $this->notlp;
	}
	
	function setNoRekening($norek){
		$this->norekening = $norek;
	}

	function getNoRekening(){
		return $this->norekening;
	}
	
	function setCreatedBy($c_by){
		$this->createdby = $c_by;
	}

	function getCreatedBy(){
		return $this->createdby;
	}

	function setCreatedOn(){
		$dateNow = date('Y-m-d H:i:s');
		$this->createdon = $dateNow;
	}

	function getCreatedOn(){
		return $this->createdon;
	}

	function setLastUpdateBy($u_by){
		$this->lastupdateby = $u_by;
	}

	function getLastUpdateBy(){
		return $this->lastupdateby;
	}

	function setLastUpdateOn(){
		$dateNow = date('Y-m-d H:i:s');
		$this->lastupdateon = $dateNow;
	}

	function getLastUpdateOn(){
		return $this->lastupdateon;
	}

	function setActive($active_val){
		$this->active = $active_val;
	}

	function getActive(){
		return $this->active;
	}
	// Call the module constructor
	function __construct(){
		parent::__construct();
	}
	
	function getBankCode()
	{
		$query = $this->db->query("select MAX(RIGHT(kodebank,3)) as kdbank from master_bank");
		$kd = "";
		
		if($query-> num_rows() > 0)
		{
			foreach($query->result() as $idbankk)
			{
				$tmpbankk = ((int)$idbankk->kdbank)+1;
				$kd = sprintf("%03s", $tmpbankk);
			}
			
		}
		else
		{
			$kd = "001";
		}
		
		$kd = "KB".$kd; 
		
		return $kd;
	}
	
	function insert(){
		/*$query = $this->db->query("select max(KodeBank) kbank from master_bank");
		$res = $query->result();
		$this->setKodeBank(($res[0]->kbank) + 1);*/
		
		$query = $this->db->query("select MAX(RIGHT(KodeBank,3)) as kdbank from master_bank");
		$kd = "";
		
		if($query-> num_rows() > 0)
		{
			foreach($query->result() as $idbankk)
			{
				$tmpbankk = ((int)$idbankk->kdbank)+1;
				$kd = sprintf("%03s", $tmpbankk);
			}
			
		}
		else
		{
			$kd = "001";
		}
			
		$kdbankok="KB".$kd;
		$this->setKodeBank($kdbankok);
		
		$data = array(
			'KodeBank' => $this->getKodeBank(),
			'Bankname' => $this->getBankName(),
			'Address' => $this->getAddress(),
			'Notlp' => $this->getNoTelp(),
			'NoRekening' => $this->getNoRekening(),
			'CreatedBy' => $this->getCreatedBy(),
			'CreatedOn' => $this->getCreatedOn(),
			'LastUpdateBy' => 'null',
			'lastupdateon' => 'null',
			'active' => '1'					
		);
		$this->db->insert('master_bank',$data);
	}
	
	function ubahmasterbank() 
	{
            $data = array(
                'Bankname' => $this->getbankname(),
                'Address' => $this->getAddress(),
                'Notlp' => $this->getNoTelp(),
                'NoRekening' => $this->getNoRekening(),
                'LastUpdateBy' => $this->getLastUpdateBy(),
                'LastUpdateOn' => $this->getLastUpdateOn()
            );
 
        $this->db->where(array('KodeBank' => $this->getKodeBank()));
        $this->db->update('master_bank', $data);
    }

    function deletemasterbank() {
        $data = array(
            'active' => '0'
        );
        $this->db->where(array('kodebank' => $this->getKodeBank()));
        $this->db->update('master_bank', $data);
    }
	
	function countGetList(){
		$this->db->select()->where(array('active'=>'1'));
		$query = $this->db->get('master_bank');
		return $query->num_rows();
	}
	
	function getList($limit,$start){
		$this->db->select()
		->where(array('active'=>'1'))
		->limit($limit,$start);
		$query = $this->db->get('master_bank');
		return $query->result();
	}
	
	function getListbank(){
		$this->db->select()
		->where(array('active'=>'1'));
		$query = $this->db->get('master_bank');
		return $query->result();
	}
	
	function getkdbank(){
		$this->db->select()->where(array('KodeBank' => $this->getKodeBank(),'active'=>'1'));
		$query = $this->db->get('master_bank');
		return $query->result();
	}
	
	function getkdbanklike(){
		$this->db->select()
		->where(array('Active'=>'1'))->like(array('KodeBank' => $this->getKodeBank()));
		$query = $this->db->get('master_bank');
		return $query->result();
	}
	
	function getnamabanklike(){
		$this->db->select()
		->where(array('Active'=>'1'))->like(array('Bankname' => $this->getBankname()));
		$query = $this->db->get('master_bank');
		return $query->result();
	}
	
	function getnorekeninglike(){
		$this->db->select()
		->where(array('Active'=>'1'))->like(array('NoRekening' => $this->getNoRekening()));
		$query = $this->db->get('master_bank');
		return $query->result();
	}
	
	function getBankPerRecord(){
		$this->db->select()->where(array('Active'=>'1'))->where(array('KodeBank' => $this->getKodeBank()));
		$query = $this->db->get('master_bank');
		return $query->result();
	}
}